package br.com.ebc.icontatos.helper;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

/**
 * 
 * @author alexfigueira Classe utilitário para tratamento dedatas e timecoce
 */
public class DataHelper {

	private static final Logger logger = Logger.getLogger(DataHelper.class);

	/**
	 * Recebe um timecode em String no formato HH:mm:ss e retorna um Date com o
	 * Time code correspondente
	 * 
	 * @param timecode
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date timeCodeStringParaData(String timecode) {
		try {
			String hora;
			String minuto;
			String segundo;
			hora = timecode.substring(0, 2);
			minuto = timecode.substring(3, 5);
			segundo = timecode.substring(6, 8);

			Timestamp tempo = new Timestamp(1900, 0, 1, Integer.parseInt(hora),
					Integer.parseInt(minuto), Integer.parseInt(segundo), 0);

			return tempo;
		} catch (Exception e) {
			StringBuilder sbError = new StringBuilder();
			sbError.append("<@@>[ErrorLog]");
			sbError
					.append("<@@>DataHelper.timeCodeStringParaData(String timecode)");
			sbError.append("<@@>TO STRING:" + e.toString());
			sbError.append("<@@> STACKTRACE: ");
			for (StackTraceElement stack : e.getStackTrace()) {
				sbError.append("\n  " + stack.toString());
			}
			logger.debug(sbError.toString());
		}
		return null;
	}

	/**
	 * Retorna um time code em String a partir de um time code em formato de
	 * data
	 * 
	 * @param timecode
	 * @return
	 */
	public static String timeCodeDataParaString(Date timecode) {
		if (timecode != null) {
			try {
				Calendar ti = new GregorianCalendar();
				ti.setTime(timecode);

				String hora = String.valueOf(ti.get(Calendar.HOUR_OF_DAY));
				String minuto = String.valueOf(ti.get(Calendar.MINUTE));
				String segundo = String.valueOf(ti.get(Calendar.SECOND));
				if (hora.length() < 2) {
					hora = "0" + hora;
				}
				if (minuto.length() < 2) {
					minuto = "0" + minuto;
				}
				if (segundo.length() < 2) {
					segundo = "0" + segundo;
				}
				String tempo = hora + ":" + minuto + ":" + segundo;

				return tempo;
			} catch (Exception e) {
				StringBuilder sbError = new StringBuilder();
				sbError.append("<@@>[ErrorLog]");
				sbError
						.append("<@@>DataHelper.timeCodeDataParaString(Date timecode)");
				sbError.append("<@@>TO STRING:" + e.toString());
				sbError.append("<@@> STACKTRACE: ");
				for (StackTraceElement stack : e.getStackTrace()) {
					sbError.append("\n  " + stack.toString());
				}
				logger.debug(sbError.toString());
			}
		}
		return null;
	}
	
	public static Date retornaDataDeString(String data) throws ParseException{
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date date = (Date)formatter.parse(data);  
		return date;
	}

	/**
	 * Recebe dois timecode inicial e final e retorna a duracao
	 * 
	 * @param timeCodeInicial
	 * @param timeCodeFinal
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date retornaDuracaoPorTimeCode(Date timeCodeInicial,
			Date timeCodeFinal) throws RuntimeException {
		try {
			if (timeCodeInicial.after(timeCodeFinal)) {
				throw new RuntimeException(
						"O tempo inicial deve ser menor que o tempo final.");
			}

			Calendar ti = new GregorianCalendar();
			ti.setTime(timeCodeInicial);

			Calendar tf = new GregorianCalendar();
			tf.setTime(timeCodeFinal);

			Timestamp timeResult = new Timestamp(1900, 0, 1, (tf
					.get(Calendar.HOUR_OF_DAY) - ti.get(Calendar.HOUR_OF_DAY)),
					(tf.get(Calendar.MINUTE) - ti.get(Calendar.MINUTE)), (tf
							.get(Calendar.SECOND) - ti.get(Calendar.SECOND)), 0);

			return timeResult;
		} catch (Exception e) {
			StringBuilder sbError = new StringBuilder();
			sbError.append("<@@>[ErrorLog]");
			sbError
					.append("<@@>DataHelper.retornaDuracaoPorTimeCode(Date timeCodeInicial, Date timeCodeFinal)");
			sbError.append("<@@>TO STRING:" + e.toString());
			sbError.append("<@@> STACKTRACE: ");
			for (StackTraceElement stack : e.getStackTrace()) {
				sbError.append("\n  " + stack.toString());
			}
			logger.debug(sbError.toString());
		}
		return null;
	}

	/**
	 * Recebe uma string de timecode somente com os numeros
	 * 
	 * @param timecode
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date timeCodeStringSemPontosParaData(String timecode) {
		try {
			String hora;
			String minuto;
			String segundo;
			hora = timecode.substring(0, 2);
			minuto = timecode.substring(2, 4);
			segundo = timecode.substring(4, 6);

			Timestamp tempo = new Timestamp(1900, 0, 1, Integer.parseInt(hora),
					Integer.parseInt(minuto), Integer.parseInt(segundo), 0);
			return tempo;
		} catch (Exception e) {
			StringBuilder sbError = new StringBuilder();
			sbError.append("<@@>[ErrorLog]");
			sbError
					.append("<@@>DataHelper.timeCodeStringSemPontosParaData(String timecode)");
			sbError.append("<@@>TO STRING:" + e.toString());
			sbError.append("<@@> STACKTRACE: ");
			for (StackTraceElement stack : e.getStackTrace()) {
				sbError.append("\n  " + stack.toString());
			}
			logger.debug(sbError.toString());
		}
		return null;
	}

	/**
	 * Calcula o proximo Time Code, que pode ser o time code final caso o
	 * "timeAtual" seja o time code inicial e o "incremento" seja a duracao. No
	 * caso de o "timeAtual" ser o time code final e o incremento ser o espaco
	 * entre uma sequencia e outra (10 segundos ou mais) o retorno sera o time
	 * code inicial da proxima sequencia.
	 * 
	 * @param timeAtual
	 * @param incremento
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date proximoTimeCode(Date timeAtual, Date incremento) {
		try {
			Calendar ti = new GregorianCalendar();
			ti.setTime(timeAtual);

			Calendar d = new GregorianCalendar();
			d.setTime(incremento);

			Timestamp timeResult = new Timestamp(1900, 0, 1, (ti
					.get(Calendar.HOUR_OF_DAY) + d.get(Calendar.HOUR_OF_DAY)),
					(ti.get(Calendar.MINUTE) + d.get(Calendar.MINUTE)), (ti
							.get(Calendar.SECOND) + d.get(Calendar.SECOND)), 0);
			return timeResult;
		} catch (Exception e) {
			StringBuilder sbError = new StringBuilder();
			sbError.append("<@@>[ErrorLog]");
			sbError
					.append("<@@>DataHelper.proximoTimeCode(Date timeAtual, Date incremento)");
			sbError.append("<@@>TO STRING:" + e.toString());
			sbError.append("<@@> STACKTRACE: ");
			for (StackTraceElement stack : e.getStackTrace()) {
				sbError.append("\n  " + stack.toString());
			}
			logger.debug(sbError.toString());
		}
		return null;
	}

	/**
	 * Calcula o Time Code anterior, que pode ser o time code inicial caso o
	 * "timeAtual" seja o time code final e o "decremento" seja a duracao. No
	 * caso de o "timeAtual" ser o time code inicial e o decremento ser o espaco
	 * entre uma sequencia e outra (10 segundos ou mais) o retorno sera o time
	 * code final da sequencia anterior.
	 * 
	 * @param timeAtual
	 * @param decremento
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date anteriorTimeCode(Date timeAtual, Date decremento) {
		try {
			Calendar ti = new GregorianCalendar();
			ti.setTime(timeAtual);

			Calendar d = new GregorianCalendar();
			d.setTime(decremento);

			Timestamp timeResult = new Timestamp(1900, 0, 1, (ti
					.get(Calendar.HOUR_OF_DAY) - d.get(Calendar.HOUR_OF_DAY)),
					(ti.get(Calendar.MINUTE) - d.get(Calendar.MINUTE)), (ti
							.get(Calendar.SECOND) - d.get(Calendar.SECOND)), 0);
			return timeResult;
		} catch (Exception e) {
			StringBuilder sbError = new StringBuilder();
			sbError.append("<@@>[ErrorLog]");
			sbError
					.append("<@@>DataHelper.anteriorTimeCode(Date timeAtual, Date decremento)");
			sbError.append("<@@>TO STRING:" + e.toString());
			sbError.append("<@@> STACKTRACE: ");
			for (StackTraceElement stack : e.getStackTrace()) {
				sbError.append("\n  " + stack.toString());
			}
			logger.debug(sbError.toString());
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	public static Date calculaHoraInicio(Date data, Date horaInicio) {

		Calendar c = new GregorianCalendar();
		Calendar dt = new GregorianCalendar();
		Calendar hi = new GregorianCalendar();

		dt.setTime(data);
		hi.setTime(horaInicio);

		c.set(Calendar.DAY_OF_MONTH, dt.get(Calendar.DAY_OF_MONTH));
		c.set(Calendar.MONTH, dt.get(Calendar.MONTH));
		c.set(Calendar.YEAR, dt.get(Calendar.YEAR));
		c.set(Calendar.HOUR_OF_DAY, hi.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, hi.get(Calendar.MINUTE));
		c.set(Calendar.SECOND, hi.get(Calendar.SECOND));

		if (horaInicio.getHours() >= 0 && horaInicio.getHours() < 5) {
			c.add(Calendar.DAY_OF_MONTH, 1);
		}

		return c.getTime();

	}

	@SuppressWarnings("deprecation")
	public static Date calculaHoraInicio(Date primeiraOcorrencia, Date data,
			Date horaInicio) {

		Calendar dt = new GregorianCalendar();
		Calendar hi = new GregorianCalendar();
		Date horaOcorrencia = null;
		Date horaPrimOcorrencia = null;
		dt.setTime(data);
		hi.setTime(horaInicio);

		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, dt.get(Calendar.DAY_OF_MONTH));
		c.set(Calendar.MONTH, dt.get(Calendar.MONTH));
		c.set(Calendar.YEAR, dt.get(Calendar.YEAR));
		c.set(Calendar.HOUR_OF_DAY, hi.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, hi.get(Calendar.MINUTE));
		c.set(Calendar.SECOND, hi.get(Calendar.SECOND));

		Date dataOcorrencia = c.getTime();

		try {
			horaOcorrencia = retornaDataFormatada(horaInicio, "hora");
			horaPrimOcorrencia = retornaDataFormatada(primeiraOcorrencia,
					"hora");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if ((dataOcorrencia.getHours() >= 0 && dataOcorrencia.getMinutes() >= 0 && dataOcorrencia
				.getSeconds() >= 0)
				&& (horaOcorrencia.before(horaPrimOcorrencia))) {
			c.add(Calendar.DAY_OF_MONTH, 1);
		}

		return c.getTime();

	}

	@SuppressWarnings("deprecation")
	public static Date calculaHoraInicioComParametros(Date data,
			Date horaInicio, int intervalo1, int intervalo2) {

		Calendar c = new GregorianCalendar();
		Calendar dt = new GregorianCalendar();
		Calendar hi = new GregorianCalendar();

		dt.setTime(data);
		hi.setTime(horaInicio);

		c.set(Calendar.DAY_OF_MONTH, dt.get(Calendar.DAY_OF_MONTH));
		c.set(Calendar.MONTH, dt.get(Calendar.MONTH));
		c.set(Calendar.YEAR, dt.get(Calendar.YEAR));
		c.set(Calendar.HOUR_OF_DAY, hi.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, hi.get(Calendar.MINUTE));
		c.set(Calendar.SECOND, hi.get(Calendar.SECOND));

		if (horaInicio.getHours() >= intervalo1
				&& horaInicio.getHours() < intervalo2) {
			c.add(Calendar.DAY_OF_MONTH, 1);
		}

		return c.getTime();

	}

	/**
	 * 0 igual 1 maior -1 menor
	 * */

	@SuppressWarnings("deprecation")
	public static Date somaHoras(Date data1, Date data2) {

		Date dataRetorno = new Date();
		dataRetorno.setHours(data1.getHours() + data2.getHours());
		dataRetorno.setMinutes(data1.getMinutes() + data2.getMinutes());
		dataRetorno.setSeconds(data1.getSeconds() + data2.getSeconds());

		return dataRetorno;

	}
	
	/**
	 * 0 igual 1 maior -1 menor
	 * */

	@SuppressWarnings("deprecation")
	public static Date subtraiHoras(Date data2, Date data1) {
		Date dataRetorno = new Date();
		dataRetorno.setHours(data1.getHours() - data2.getHours());
		dataRetorno.setMinutes(data1.getMinutes() - data2.getMinutes());
		dataRetorno.setSeconds(data1.getSeconds() - data2.getSeconds());
		return dataRetorno;

	}
	
	@SuppressWarnings("deprecation")
	public static Date subtraiDataHora(Date data2, Date data1) {
		Date dataRetorno = new Date(data1.getTime() - data2.getTime());
		return dataRetorno;

	}
	
	@SuppressWarnings("deprecation")
	public static Date subtraiHoraData(Date data2, Date data1) {
		Date dataRetorno = new Date(data2.getTime() - data1.getTime());
		return dataRetorno;

	}
	
/*	@SuppressWarnings("deprecation")
	public static Date subtraiHoraData(Date data2, Date data1, Date hora2, Date hora1) {
		//SETANDO HORA NA DATA2
		data2.setHours(hora2.getHours());
		data2.setMinutes(hora2.getMinutes());
		data2.setSeconds(hora2.getSeconds());
		//SETANDO HORA NA DATA1
		data1.setHours(hora1.getHours());
		data1.setMinutes(hora1.getMinutes());
		data1.setSeconds(hora1.getSeconds());       
		Date diferenca = new Date(data2.getTime() - data1.getTime());  
		return diferenca;

	}*/

	public static Date retornaHoraZerada() {
		Date tempoZerado = new Date();
		tempoZerado.setHours(0);
		tempoZerado.setMinutes(0);
		tempoZerado.setSeconds(0);
		return tempoZerado;
	}
	public static Date retornaHoraZerada(Date tempoZerado) {
		
		tempoZerado.setHours(0);
		tempoZerado.setMinutes(0);
		tempoZerado.setSeconds(0);
		return tempoZerado;
	}

	public static Calendar retornaCalendar() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}

	// tentar mecher nisso depois
	public static Date retornaDate(Date data, Date horario) {

		Calendar c = new GregorianCalendar();
		Calendar dt = new GregorianCalendar();
		Calendar hi = new GregorianCalendar();

		dt.setTime(data);
		hi.setTime(horario);

		c.set(Calendar.DAY_OF_MONTH, dt.get(Calendar.DAY_OF_MONTH));
		c.set(Calendar.MONTH, dt.get(Calendar.MONTH));
		c.set(Calendar.YEAR, dt.get(Calendar.YEAR));
		c.set(Calendar.HOUR_OF_DAY, hi.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, hi.get(Calendar.MINUTE));
		c.set(Calendar.SECOND, hi.get(Calendar.SECOND));

		return c.getTime();
	}

	/**
	 * 
	 * @param data
	 * @param tipo
	 *            {DATA = dd/MM/yyyy , HORA = hh:mm:ss, ANOMES = yyyy/MM , completo = dd/MM/yyyy hh:mm:ss}
	 * @return
	 */
	public static String retornaDataFormatadaEmString(Date data, String tipo) {
		SimpleDateFormat sdf = null;
		String diaSemanas = null;
		String dataFormatada = null;
		if (tipo.equalsIgnoreCase("data")) {
			sdf = new SimpleDateFormat("dd/MM/yyyy");
		}
		if (tipo.equalsIgnoreCase("completo")) {
			sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		}
		if (tipo.equalsIgnoreCase("hora")) {
			sdf = new SimpleDateFormat("HH:mm:ss");
		}
		if (tipo.equalsIgnoreCase("horaMinutoSegundo")) {
			sdf = new SimpleDateFormat("HH:mm");
		}
		if (tipo.equalsIgnoreCase("anomes")) {
			sdf = new SimpleDateFormat("yyyy/MM");
		}
		
		if (tipo.equalsIgnoreCase("dataComp")) {
			sdf = new SimpleDateFormat("dd/MM/yyyy");
			Calendar dataC = new GregorianCalendar();
			dataC.setTime(data);
			int diaSemana = dataC.get(Calendar.DAY_OF_WEEK);
			switch (diaSemana) {
			case 1: {
				diaSemanas = "Domingo";
				break;
			}
			case 2: {
				diaSemanas = "Segunda-feira";
				break;
			}
			case 3: {
				diaSemanas = "Terça-feira";
				break;
			}
			case 4: {
				diaSemanas = "Quarta-feira";
				break;
			}
			case 5: {
				diaSemanas = "Quinta-feira";
				break;
			}
			case 6: {
				diaSemanas = "Sexta-feira";
				break;
			}
			case 7: {
				diaSemanas = "Sábado";
				break;
			}
			}// fim switch
		}// fim dataComp
		dataFormatada = sdf.format(data);
		if (tipo.equalsIgnoreCase("dataComp")) {
			dataFormatada += " - " + diaSemanas;
		}// fim if dataComp

		sdf = null;
		return dataFormatada;
	}

	/**
	 * 
	 * @param data
	 * @param tipo
	 *            {DATA = dd/MM/yyyy , HORA = hh:mm:ss, ANOMES = yyyy/MM}
	 * @return
	 * @throws ParseException
	 */
	public static Date retornaDataFormatada(Date data, String tipo)
			throws ParseException {
		SimpleDateFormat sdf = null;
		if (tipo.equalsIgnoreCase("data")) {
			sdf = new SimpleDateFormat("dd/MM/yyyy");
		}
		if (tipo.equalsIgnoreCase("hora")) {
			sdf = new SimpleDateFormat("HH:mm:ss");
		}
		if (tipo.equalsIgnoreCase("anomes")) {
			sdf = new SimpleDateFormat("yyyy/MM");
		}

		String dataFormatada = sdf.format(data);
		Date dat = sdf.parse(dataFormatada);
		sdf = null;
		return dat;
	}

	public static String retornaDataPorExtenso(Date data) {
		String mes = null;
		switch (data.getMonth()) {
		case 0:
			mes = "Janeiro";
		case 1:
			mes = "Fevereiro";
		case 2:
			mes = "Março";
		case 3:
			mes = "Abril";
		case 4:
			mes = "Maio";
		case 5:
			mes = "Junho";
		case 6:
			mes = "Julho";
		case 7:
			mes = "Agosto";
		case 8:
			mes = "Setembro";
		case 9:
			mes = "Outubro";
		case 10:
			mes = "Novembro";
		case 11:
			mes = "Dezembro";
		}
		String dataExtenso = data.getDay() + " de " + mes + " de "
				+ data.getYear();

		return dataExtenso;
	}

	public static int retornaDuracaoEmSegundos(Date data) {
		int hora = data.getHours();
		int min = data.getMinutes();
		int sec = data.getSeconds();

		return (hora * 3600) + (min * 60) + sec;
	}

	public static double retornaDuracaoEmSegundosDougle(Date data) {
		double hora = data.getHours();
		double min = data.getMinutes();
		double sec = data.getSeconds();

		return (hora * 3600) + (min * 60) + sec;
	}

	public static Date retornaData(int dia, int mes, int ano) {
		GregorianCalendar dg = new GregorianCalendar(ano, mes, dia);
		Date data = dg.getTime();
		return data;
	}// fim retornaData

	/**
	 * @author leonardoneves
	 * @param data
	 * @return
	 */
	public static Calendar transformaDateCalendar(Date data) {
		Calendar dataC = new GregorianCalendar();
		dataC.setTime(data);
		return dataC;
	}// fim transformaDateCalendar

	/**
	 * @author leonardoneves
	 * @return data
	 */
	public static Date voltarDataVazia() {
		GregorianCalendar cal = new GregorianCalendar();
		cal.clear();
		cal.set(Calendar.DAY_OF_MONTH, 0);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.YEAR, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}// fim voltarDataVazia
	
	/**
	 * @author leonardoneves
	 * @return Calendar
	 */
	public static Date voltarCalendarVazio() {
		GregorianCalendar cal = new GregorianCalendar();
		cal.clear();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.YEAR, 1970);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}// fim voltarDataVazia

	/**
	 * @author leonardoneves
	 * @param data
	 * @return
	 */
	public static Date retornaDataSemTempo(Date data) {
		GregorianCalendar calData = new GregorianCalendar();
		calData.clear();
		calData.setTime(data);
		GregorianCalendar cal = new GregorianCalendar();
		cal.clear();
		cal.set(Calendar.DAY_OF_MONTH, calData.get(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.MONTH, calData.get(Calendar.MONTH));
		cal.set(Calendar.YEAR, calData.get(Calendar.YEAR));
		return cal.getTime();
	}// fim voltarDataVazia

	/**
	 * @author leonardoneves
	 * @param data
	 * @return
	 */
	public static Date retornaTempoSemData(Date data) {
		GregorianCalendar calData = new GregorianCalendar();
		calData.clear();
		calData.setTime(data);
		GregorianCalendar cal = new GregorianCalendar();
		cal.clear();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.YEAR, 1970);
		cal.set(Calendar.HOUR_OF_DAY, calData.get(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, calData.get(Calendar.MINUTE));
		cal.set(Calendar.SECOND, calData.get(Calendar.SECOND));
		return cal.getTime();
	}// fim voltarDataVazia
	
	/**
	 * @author leonardoneves
	 * @param data
	 * @return
	 */
	public static Date retornaTempoComDataAtual(Date data, Date tempo) {
		//tempo
		GregorianCalendar calData = new GregorianCalendar();
		calData.clear();
		calData.setTime(tempo);
		GregorianCalendar cal = new GregorianCalendar();
		cal.clear();
		cal.set(Calendar.HOUR_OF_DAY, calData.get(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, calData.get(Calendar.MINUTE));
		cal.set(Calendar.SECOND, calData.get(Calendar.SECOND));
		//data
		calData.clear();
		calData.setTime(data);
		cal.set(Calendar.DAY_OF_MONTH, calData.get(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.MONTH, calData.get(Calendar.MONTH));
		cal.set(Calendar.YEAR, calData.get(Calendar.YEAR));
		return cal.getTime();
	}// fim voltarDataVazia

	public static String retornaDiaSemana(Date data){
	    Calendar calendario = new GregorianCalendar();
	    calendario.setTime(data);
	    int diaSemana = calendario.get(Calendar.DAY_OF_WEEK);  
	    return pesquisarDiaSemana(diaSemana);  
	  }//fim retornarDiaSemana
	  
	  public static String pesquisarDiaSemana(int diaSemana){
	    String sDiaSemana = null;  
	    switch (diaSemana){ 
		    case 1:{
		      sDiaSemana = "Domingo";  
		      break;  
		    }
		    case 2:{
		      sDiaSemana = "Segunda-feira";  
		      break;  
		    }
		    case 3:{  
		      sDiaSemana = "Terça-feira";  
		      break;  
		    }  
		    case 4:{  
		      sDiaSemana = "Quarta-feira";  
		      break;  
		    }  
		    case 5:{  
		      sDiaSemana = "Quinta-feira";  
		      break;  
		    }  
		    case 6:{  
		      sDiaSemana = "Sexta-feira";  
		      break;  
		    }  
		    case 7:{  
		      sDiaSemana = "Sábado";  
		      break;  
		    }
	    }//fun switch  
	    return sDiaSemana;  
	  }//fim pesquisarDiaSemana
	public static Date converteStringParaData(String data){
	    DateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
	    Date date = null;
		try {
			date = (Date)formatter.parse(data);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	    return date;
	}
}
